package bing.commons.xmloperator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.DocumentException;

import com.google.common.io.Files;

/*
 * 读取某一个classpath文件，读取文件内的path属性值，然后把包含的jar包拷贝到指定的目录下
 */
public class JarCopy {

	private static List<String> pathvarlist=new ArrayList();
	private static final String destpath="C:\\Users\\LBH\\Desktop\\jarexports";
	public static void main(String[] args) throws DocumentException, IOException {
		// TODO Auto-generated method stub
		//找出制定目录下，所有的jar文件，并拷贝到指定目录。
		findclasspathFile("D:/projects/palantirall/atlasdb-0.3.30");

	}
	/*
	 * 找出制定目录下，所有的jar文件，并拷贝到指定目录。
	 */
	public static void findclasspathFile(String fileName) throws IOException{
        File file = new File(fileName);  
        int filecount=0;
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                String name = files[i].getName();  
                if (name.trim().toLowerCase().endsWith(".jar")) {  
                    System.out.println(files[i].getPath() + "\t");  
                    //classpathfilelist.add(files[i].getPath());//添加到list中
                    copyJarFile(files[i].getPath(),destpath);
                    filecount++;
                }  
                if (files[i].isDirectory()) {  
                    String path = files[i].getPath();  
                    findclasspathFile(path);  
                }  
            }  
        } 
        System.out.println("this time: "+filecount+" files copy completed");
	}
	

	public static void copyJarFile(String sourcepath,String destpath) throws IOException{
	       //复制移动文件  

	        File from=new File(sourcepath);  
	        File to=new File(destpath+"/"+from.getName());  
	        Files.copy(from, to);  

     //Files.move(from, to
	}
	
	public static void copyJarFile(File sourceFile,String destpath) throws IOException{
	       //复制移动文件  

	        //File from=new File(sourcepath);  
	        File to=new File(destpath+"/"+sourceFile.getName());  
	        Files.copy(sourceFile, to);  

	}
}
